package com.lm.controller;

import com.lm.service.LmProductGroupService;
import com.meirengu.common.StatusCode;
import com.meirengu.controller.BaseController;
import com.meirengu.model.Page;
import com.meirengu.model.Result;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("productGroup")
public class lmProductGroupController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(lmProductGroupController.class);

    @Autowired
    LmProductGroupService lmProductGroupService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Result listMultiple(@RequestParam(value = "page_num", required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "page_size", required = false, defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "sortby", required = false, defaultValue = "create_date") String sortBy,
                               @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
                               @RequestParam(value = "status", required = false, defaultValue = "1") Integer status) {

        try {
            // 目前用城市名称，后期改为用行政代码查询
            Map<String, Object> paramMap = new HashedMap();
            Page page = new Page();
            page = super.setPageParams(pageNum, pageSize);
            if (StringUtils.isNotBlank(sortBy)) {
                paramMap.put("sortBy", sortBy);
            }
            if (StringUtils.isNotBlank(order)) {
                paramMap.put("order", order);
            }
            paramMap.put("status", status);
            page = lmProductGroupService.getListByPage(page, paramMap);
            return super.setResult(StatusCode.OK, page, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            String params = "";
            logger.error("lmProductGroupController listMultiple throws Exceprion :{} happens. params:{}.", ExceptionUtils.getFullStackTrace(e), params);
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }

    }
}
